package com.simon.hse.activity.contractor;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.EnterInput;
import com.simon.hse.http.entity.ExitOutput;
import com.simon.hse.utils.DateTimeUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import retrofit2.Call;

/**
 * 入场确认
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_enter)
public class EnterActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.tv_contractor_name)
    TextView mTv_contractor_name;

    @ViewInject(R.id.tv_contractor_person)
    TextView mTv_contractor_person;

    @ViewInject(R.id.et_contractor_area)
    EditText mEt_contractor_area;

    @ViewInject(R.id.tv_contractor_time)
    TextView mTv_contractor_time;

    private List<String> mListContractorPerson = new ArrayList<>();
    private String companyName, companyId;
    private String personName, personId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("入场确认");

        // 默认当前时间
        mTv_contractor_time.setText(DateTimeUtils.getCurrentYMDHM());
    }

    @Event(value = {R.id.iv_common_left})
    private void back(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
        }
    }

    @Event(value = {R.id.ll_contractor_name, R.id.ll_contractor_person, R.id.iv_contractor_time, R.id.tv_contractor_enter})
    private void clickButton(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.ll_contractor_name:
                intent.setClass(EnterActivity.this, ConstractorCompanyActivity.class);
                bundle.putString("TITLE", "承包商选取");
                bundle.putString("NAME", companyName);
                bundle.putString("ID", companyId);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_contractor_person:
                intent.setClass(EnterActivity.this, ConstractorPersonActivity.class);
                bundle.putString("TITLE", "承包商人员选取");
                if (TextUtils.isEmpty(companyName) || TextUtils.isEmpty(companyId)) {
                    ToastUtils.getInstance().showShortToastBottom("请先选择承包商名称");
                    return;
                }
                bundle.putString("NAME", companyName);
                bundle.putString("ID", companyId);
                bundle.putString("NAME1", personName);
                bundle.putString("ID1", personId);
                intent.putExtras(bundle);
                startActivityForResult(intent, 2);
                break;
            case R.id.iv_contractor_time:
                initEnterTime();
                break;
            case R.id.tv_contractor_enter:
                submit();
                break;
        }
    }

    /**
     * 入场时间
     */
    private void initEnterTime() {
        Calendar selectedDate = Calendar.getInstance();
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                mTv_contractor_time.setText(sdf_ymd.format(date));
            }
        })
                .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                .setTitleSize(20)//标题文字大小
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.WHITE)//确定按钮文字颜色
                .setCancelColor(Color.WHITE)//取消按钮文字颜色
                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setLabel("年", "月", "日", "时", "分", "")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();

        pvTime.show();
    }

    /**
     * 提交
     */
    private void submit() {
        String name = mTv_contractor_name.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.getInstance().showShortToastBottom("请选择承包商名称!");
            return;
        }
        String person = mTv_contractor_person.getText().toString();
        if (TextUtils.isEmpty(person)) {
            ToastUtils.getInstance().showShortToastBottom("请选择承包商人员姓名!");
            return;
        }
        String area = mEt_contractor_area.getText().toString();
        String time = mTv_contractor_time.getText().toString();
        if (TextUtils.isEmpty(area)) {
            ToastUtils.getInstance().showShortToastBottom("请选择入场区域!");
            return;
        }

        EnterInput enterInput = new EnterInput();
        enterInput.setCbsListId(companyId);
        enterInput.setCreateFactory("1");
        String[] split = personId.split(",");
        List<String> list = Arrays.asList(split);
        enterInput.setCbsUserIds(list);
        enterInput.setRegion(area);
        enterInput.setAdmissionTime(time);

        Call<String> call = HttpManager.getInstance().requestService().CONTRACTOR_ENTER(HttpManager.getInstance().requestBody(enterInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                ExitOutput exitOutput  = new Gson().fromJson(result, ExitOutput.class);
                if (exitOutput.getResult().equals("Success")) {
                    ToastUtils.getInstance().showShortToastBottom("提交成功!");
                    finishActivity(EnterActivity.this);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(exitOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bundle bundle = data.getExtras();
        if (bundle == null)
            return;
        switch (requestCode) {
            case 1:
                companyName = bundle.getString("NAME");
                companyId = bundle.getString("ID");
                mTv_contractor_name.setText(companyName);
                break;
            case 2:
                personName = bundle.getString("NAME");
                personId = bundle.getString("ID");
                mTv_contractor_person.setText(personName.substring(0, personName.length() - 1));
                break;
        }
    }
}
